package com.liqing.movecomment.config;

import com.liqing.movecomment.vo.model.Result;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class CustomExceptionHandler {
    //<editor-fold defaultstate="collapsed" desc="delombok">
    @SuppressWarnings("all")
    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CustomExceptionHandler.class);
    //</editor-fold>

    // 参数校验的异常处理
    @ExceptionHandler(BindException.class)
    public Result validatedBindException(BindException e) {
        String message = e.getAllErrors().get(0).getDefaultMessage();
        log.error("异常:", e);
        return Result.fail(message);
    }

    // 控制器层的全局异常处理
    // ExceptionHandler 注解的作用是：要处理哪种类信息的异常
    @ExceptionHandler(Exception.class)
    public Result handlerException(Exception e) {
        log.error("异常:", e);
        return Result.fail(e.getMessage());
    }
}
